1

I use JSON.NET and I would like to parse the following object which I get from a WebService. Can someone post an example on how to do that?

@"{""MessageType"":0,
   ""Message"":""Success"",
   ""Value"":[
              {""listId"":1,
               ""listName"":""DemoList"",
               ""itemInList"":[
                    {
                     ""fromDate"":""\/Date(1228946400000)\/"",
                     ""fromLocation"":null,
                     ""toLocation"":null,
                     ""originalRequest"":""water"",
                     ""creationDate"":""\/Date(1339448400000)\/"",
                     ""typeId"":1
                    },
                    {
                     ""fromDate"":null,
                     ""fromLocation"":null,
                     ""toLocation"":null,
                     ""originalRequest"":""gala"",
                     ""creationDate"":""\/Date(1304370000000)\/"",
                     ""typeId"":1
                    }
              ]}
    ]}"

JSON Object

{
  "MessageType":0,
  "Message":"UserLists",
  "Value":
          [
            {
              "listId":1,
              "listName":"DemoList",
              "itemInList" 
                    [
                      {
                         "fromDate":"\/Date(1228946400000)\/",
                         "fromLocation":null,
                         "toLocation":null,
                         "originalRequest":"water",
                         "creationDate":"\/Date(1339448400000)\/",
                         "typeId":1
                      },
                      {
                         "fromDate":null,
                         "fromLocation":null,
                         "toLocation":null,
                         "originalRequest":"gala",
                         "creationDate":"\/Date(1304370000000)\/",
                         "typeId":1
                       }
                  ],
                  "numberOfItems":2
              }
          ]
     }

Thanks.

1
  • That is the string-literal that would be in C# source for such JSON. It's not so useful to show the string-literal form; if anything is to be shown, show the real JSON value, unless there is a reason to do otherwise. Anyway, after downloading the distributable, read the CHM helpfile that comes with it. Commented Apr 1, 2012 at 8:53

2 Answers 2

7

You need to create some entity like this:

public class Entity
{
    public int MessageType { get; set; }
    public string Message { get; set; }
    public List<EntityValue> Value { get; set; }
}

public class EntityValue
{
    public int listId { get; set; }
    public string listName { get; set; }
    public List<ItemInList> itemInList { get; set; }
}

public class ItemInList
{
    public DateTime? fromDate { get; set; }
    public string fromLocation { get; set; }
    public string toLocation { get; set; }
    public string originalRequest { get; set; }
    public DateTime creationDate { get; set; }
    public int typeId { get; set; }
}

The entity must has the same structure like the json data. And you can call the Method:

JsonConvert.DeserializeObject<Entity>(json);

If it has any exception,you need to adjust the entities until it works.

Sign up to request clarification or add additional context in comments.

1 Comment

@Dozer, can you be more specific on how to call Item in the end please?
0

Please read the below link for parsi in metro style application. http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh770287.aspx

1 Comment

Can you summarize the info that you are linking to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.